package hot100;

import comment.TreeNode;

/**
 * @Author: wzp
 * @CreateTime: 2024-09-14  11:30
 * @Description: 二叉树的最大直径
 * @Version: 1.0
 */
public class hot_543 {
    public static int max=Integer.MIN_VALUE;
    public int diameterOfBinaryTree(TreeNode root) {
        max=Integer.MIN_VALUE;
        if(root==null){
            return 0;
        }
        dfs(root);
        return max;

    }
    public void dfs(TreeNode treeNode){
        if(treeNode==null){
            return;
        }
        dfs(treeNode.left);
        dfs(treeNode.right);
        int l=treeNode.left==null?0:treeNode.left.val+1;
        int r=treeNode.right==null?0:treeNode.right.val+1;
        if(l+r>max){
            max=l+r;
        }
        treeNode.val=l>r?l:r;
    }
}
